package cn.wzc.crystal.platform.query.config;

import cn.wzc.crystal.platform.query.QueryCompiler;
import cn.wzc.crystal.platform.query.QueryProvider;
import cn.wzc.crystal.platform.query.QueryService;
import cn.wzc.crystal.platform.query.impl.DefaultQueryCompiler;
import cn.wzc.crystal.platform.query.impl.DefaultQueryProvider;
import cn.wzc.crystal.platform.query.impl.DefaultQueryService;
import com.github.drinkjava2.jdialects.Dialect;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
@RequiredArgsConstructor
public class QueryConfigure {

	private final QueryProperties properties;

	@Bean
	@ConditionalOnMissingBean(QueryProvider.class)
	public QueryProvider queryProvider() {
		final DefaultQueryProvider queryProvider = new DefaultQueryProvider();
		queryProvider.setCacheable(properties.isCacheable());
		queryProvider.setStuff(properties.getTemplateStuff());
		queryProvider.setResourceDir(properties.getResourceDir());
		return queryProvider;
	}


	@Bean
	@ConditionalOnMissingBean(QueryCompiler.class)
	public QueryCompiler queryCompiler() {
		return new DefaultQueryCompiler();
	}

	@Bean
	@ConditionalOnMissingBean(QueryService.class)
	public QueryService queryService(QueryProvider provider,
									 QueryCompiler compiler,
									 JdbcTemplate jdbcTemplate) {
		return new DefaultQueryService(new Dialect(properties.getDialect().name()),
			provider,
			compiler,
			jdbcTemplate);
	}

}
